package com.hadoop.mapreduce.wordcount;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class WordCountMapReducer extends Configured implements Tool {

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        private Text outputKey = new Text();
        private final static LongWritable outputValue = new LongWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            String[] words = value.toString().trim().split(" ");
            for (String word : words) {
                if (StringUtils.isNotEmpty(word)) {
                    outputKey.set(word);
                    context.write(outputKey, outputValue);
                }
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            long sum = 0;
            for (LongWritable value : values) {
                sum += value.get();
            }
            context.write(key, new LongWritable(sum));

        }
    }


    public int run(String args[]) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, WordCountMapReducer.class.getSimpleName());
        job.setJarByClass(WordCountMapReducer.class);

        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setCombinerClass(WordCountReducer.class);
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean isSucceed = job.waitForCompletion(true);
        return isSucceed ? 1 : 0;
    }

    public static void main(String[] args) throws Exception {
        args = new String[]{
                "hdfs://172.16.1.229:8020/test/test.txt",
                "hdfs://172.16.1.229:8020/test/word-count"};
        int status = ToolRunner.run(new WordCountMapReducer(), args);
        System.out.println(status);
    }
}
